using System;
using System.Collections.Generic;
using System.IO;
using Nakio.Framework.Communication;
using Nakio.SystemAdministration.CustomClasses;

namespace Nakio.SystemAdministration.Commands
{
    [Serializable]
    public class GetCustomFileInfosCommand : CommandBase
    {
        private readonly string _directoryPath;

        public GetCustomFileInfosCommand(string directoryPath)
        {
            _directoryPath = directoryPath;    
        }

        public override object Execute()
        {
            return GetCustomFileInfos();           
        }

        public List<CustomFileInfo> GetCustomFileInfos()
        {
            var list = new List<CustomFileInfo>();
            var dirInfo = new DirectoryInfo(_directoryPath);
            var getFileSystemInfos = dirInfo.GetFileSystemInfos();

            foreach (var fileSystemInfo in getFileSystemInfos)
            {
                if (fileSystemInfo is DirectoryInfo)
                {
                    var info = (DirectoryInfo)fileSystemInfo;
                    list.Add(new CustomFileInfo(info.FullName, info.Name, null, info.LastWriteTime));
                }
                else if (fileSystemInfo is FileInfo)
                {
                    var info = (FileInfo)fileSystemInfo;
                    list.Add(new CustomFileInfo(info.FullName, info.Name, info.Length, info.LastWriteTime));
                }
            }

            return list;
        }
    }
}